package com.errorfinding.tabbitmqDemo.config;

import com.alibaba.druid.sql.visitor.functions.Bin;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 发布确认(高级)
 * 配置类
 */
@Configuration
public class ConfirmConfig {
    //交换机名称
    public static final String confirm_exchange_name = "confirm_exchange";
    //队列名称
    public static final String confirm_queue_name = "confirm_queue";
    //routingkey
    public static final String confirm_routing_key = "key1";

    //备份 交换机
    public static final String backup_exchange_name = "backup_exchange";
    //备份 队列
    public static final String backup_queue_name = "backup_queue";
    //备份 报警队列
    public static final String warning_queue_name = "warning_queue";

    //交换机声明
    @Bean
    public DirectExchange confirmExchange(){
        //当主交换机出问题的的时候路由到备份交换机
        return ExchangeBuilder.directExchange(confirm_exchange_name).durable(true).withArgument("alternate-exchange",backup_exchange_name).build();
        //return new DirectExchange(confirm_exchange_name);
    }

    //队列声明
    @Bean
    public Queue confirmQueue(){
        return QueueBuilder.durable(confirm_queue_name).build();
    }

    //绑定
    @Bean
    public Binding queueBindingExchange(@Qualifier("confirmQueue") Queue confirmQueue,
                                        @Qualifier("confirmExchange") DirectExchange confirmExchange){

        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(confirm_routing_key);
    }


    //备份 交换机声明
    @Bean
    public FanoutExchange backupExchange(){
        return new FanoutExchange(backup_exchange_name);
    }
    //备份 队列
    @Bean
    public Queue backupQueue(){
        return QueueBuilder.durable(backup_queue_name).build();
    }
    //备份 报警队列
    @Bean
    public Queue warningQueue(){
        return QueueBuilder.durable(warning_queue_name).build();
    }

    //备份绑定
    @Bean
    public Binding backupQueueBindingBackupExchange(@Qualifier("backupQueue") Queue backupQueue,
                                                    @Qualifier("backupExchange") FanoutExchange backupExchange){

        return BindingBuilder.bind(backupQueue).to(backupExchange);
    }

    @Bean
    public Binding warningQueueBindingBackupExchange(@Qualifier("warningQueue") Queue warningQueue,
                                                    @Qualifier("backupExchange") FanoutExchange backupExchange){

        return BindingBuilder.bind(warningQueue).to(backupExchange);
    }



}
